using System;
using System.Collections.Generic;
using System.Linq;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestDMToClassDefConverter
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [Test]
        public void Test_ClassConversion()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            dmClass.MappedTableName = TestUtilsShared.GetRandomString();
            dmClass.DisplayName = TestUtilsShared.GetRandomString();

            //---------------Execute Test ----------------------
            IClassDef classDef = new DmToClassDefsConverter().Convert(dmClass);

            //---------------Test Result -----------------------
            Assert.IsNotNull(classDef);
            Assert.AreEqual(dmClass.DisplayName, classDef.DisplayName);
            Assert.AreEqual(dmClass.MappedTableName, classDef.TableName);
        }

        [Test]
        public void Test_SuperClassConversion_NoIdentity()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMClass dmSuperClassClass = TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);

            DMInheritanceRelationship superClass = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            superClass.SubClass = dmClass;
            superClass.SuperClass = dmSuperClassClass;
            superClass.InheritanceType = ORMapping.SingleTableInheritance;
            superClass.Discriminator = TestUtilsShared.GetRandomString();

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDefs.Count);
            Assert.IsNotNull(classDef.SuperClassDef);
            Assert.AreEqual(dmSuperClassClass.ClassNameBO, classDef.SuperClassDef.ClassName);
            Assert.AreEqual(dmSuperClassClass.Assembly.Name, classDef.SuperClassDef.AssemblyName);
            Assert.AreEqual(ORMapping.SingleTableInheritance, classDef.SuperClassDef.ORMapping);
            Assert.AreEqual(superClass.Discriminator, classDef.SuperClassDef.Discriminator);
        }

        [Test]
        public void Test_SuperClassConversion_WithIdentity()
        {
            //---------------Set up test pack-------------------
            DMClass subClass = TestUtilsDMClass.CreateSavedDMClassWithOID(1);
            DMClass superClass = TestUtilsLogic.CreateClassWithOneGuidProperty(subClass.Assembly);

            DMInheritanceRelationship inheritanceRelationship = TestUtilsShared.CreateUnsavedValidDMInheritanceRelationship();
            inheritanceRelationship.SubClass = subClass;
            inheritanceRelationship.SuperClass = superClass;
            inheritanceRelationship.InheritanceType = ORMapping.ClassTableInheritance;
            var identityProperty = subClass.NonOIDProperties[0];
            inheritanceRelationship.IdentityProperty = identityProperty;
            
            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) subClass.Solution);
            IClassDef classDef = classDefs[subClass.Assembly.Name, subClass.ClassNameBO];

            //---------------Test Result -----------------------
            SuperClassDef superClassDef = (SuperClassDef) classDef.SuperClassDef;
            Assert.AreEqual(ORMapping.ClassTableInheritance, superClassDef.ORMapping);
            Assert.AreEqual(identityProperty.PropertyName, superClassDef.ID);
        }

        [Test]
        public void Test_PropertyConversion_Type()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            dmClass.Properties[0].DisplayName = TestUtilsShared.GetRandomString();

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(1, classDef.PropDefcol.Count);
            PropDef propDef = (PropDef) classDef.PropDefcol[dmClass.Properties[0].PropertyName];
            Assert.AreSame(typeof (Guid), propDef.PropertyType);
            Assert.AreEqual("System.Guid", propDef.PropertyTypeName);
            Assert.AreEqual(dmClass.Properties[0].DisplayName, propDef.DisplayName);
            Assert.IsNull(dmClass.Properties[0].DefaultValue);
            Assert.IsInstanceOf(typeof(NullLookupList), propDef.LookupList);
        }

        [Test]
        public void Test_PropertyConversion_StringAttributes()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            property.PropertyType = TestUtilsLogic.GetStringPropertyType();
            property.Compulsory = true;
            property.ReadWriteRule = PropReadWriteRule.WriteNew;
            property.MappedDBColumnName = TestUtilsShared.GetRandomString();
            property.DefaultValue = TestUtilsShared.GetRandomString();
            property.Description = TestUtilsShared.GetRandomString();
            property.Length = TestUtilsShared.GetRandomInt(200);
            property.KeepValuePrivate = true;

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.PropDefcol.Count);
            IPropDef propDef = classDef.PropDefcol[property.PropertyName];
            Assert.IsTrue(propDef.Compulsory);
            Assert.AreEqual(PropReadWriteRule.WriteNew, propDef.ReadWriteRule);
            Assert.AreEqual(property.MappedDBColumnName, propDef.DatabaseFieldName);
            Assert.AreEqual(property.DefaultValue, propDef.DefaultValue);
            Assert.AreEqual(property.Description, propDef.Description);
            Assert.AreEqual(property.Length, propDef.Length);
            Assert.IsTrue(propDef.KeepValuePrivate);
        }


        [Test]
        public void Test_PropertyConversion_IntAttributes()
        {
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            @property.PropertyType = TestUtilsLogic.GetIntegerPropertyType();
            @property.DefaultValue = TestUtilsShared.GetRandomInt(200).ToString();
            @property.AutoIncrementing = true;

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.PropDefcol.Count);
            IPropDef propDef = classDef.PropDefcol[@property.PropertyName];
            Assert.AreEqual(Convert.ToInt32(@property.DefaultValue), propDef.DefaultValue);
            Assert.IsTrue(propDef.AutoIncrementing);
        }

        [Test]
        public void Test_SimpleLookupList()
        {
            //---------------Set up test pack-------------------
            DMLookupListSimple lookupListSimple = new DMLookupListSimple();
            DMListItem dmListItem1 = new DMListItem();
            dmListItem1.Display = TestUtilsShared.GetRandomString();
            dmListItem1.Value = TestUtilsShared.GetRandomString();
            DMListItem dmListItem2 = new DMListItem();
            dmListItem2.Display = TestUtilsShared.GetRandomString();
            dmListItem2.Value = TestUtilsShared.GetRandomString();
            lookupListSimple.ListItems.Add(dmListItem1);
            lookupListSimple.ListItems.Add(dmListItem2);
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            property.PropertyType = TestUtilsLogic.GetStringPropertyType();
            property.LookupList = lookupListSimple;

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.PropDefcol.Count);
            IPropDef propDef = classDef.PropDefcol[property.PropertyName];
            ILookupList lookupList = propDef.LookupList;
            Assert.IsNotNull(lookupList);
            Assert.IsInstanceOf(typeof(SimpleLookupList), lookupList);
            Dictionary<string, string> dictionary = lookupList.GetLookupList();
            Assert.AreEqual(2, dictionary.Count);
            Assert.AreEqual(dmListItem1.Value, dictionary[dmListItem1.Display]);
            Assert.AreEqual(dmListItem2.Value, dictionary[dmListItem2.Display]);
            Dictionary<string, string> idValueLookupList = lookupList.GetIDValueLookupList();
            Assert.AreEqual(2, idValueLookupList.Count);
        }

        [Test]
        public void Test_BusinessObjectLookupList()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            property.PropertyType = TestUtilsLogic.GetStringPropertyType();
            DMLookupListBusinessObject dmLookupListBusinessObject = new DMLookupListBusinessObject();
            dmLookupListBusinessObject.ClassName = TestUtilsShared.GetRandomString();
            dmLookupListBusinessObject.AssemblyName = TestUtilsShared.GetRandomString();
            dmLookupListBusinessObject.Timeout = TestUtilsShared.GetRandomInt();
            DMSortOrder dmSortOrder = new DMSortOrder();
            DMSortProperty dmSortProperty = new DMSortProperty();
            dmSortProperty.PropertyName = TestUtilsShared.GetRandomString();
            dmSortOrder.DMSortProperties.Add(dmSortProperty);
            dmLookupListBusinessObject.DMSortOrder = dmSortOrder;
            property.LookupList = dmLookupListBusinessObject;
            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];
            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.PropDefcol.Count);
            IPropDef propDef = classDef.PropDefcol[property.PropertyName];
            ILookupList lookupList = propDef.LookupList;
            Assert.IsNotNull(lookupList);
            Assert.IsInstanceOf(typeof(BusinessObjectLookupList), lookupList);
            BusinessObjectLookupList boLookupList = ((BusinessObjectLookupList) lookupList);
            Assert.AreEqual(dmLookupListBusinessObject.ClassName, boLookupList.ClassName);
            Assert.AreEqual(dmLookupListBusinessObject.AssemblyName, boLookupList.AssemblyName);
            Assert.AreEqual(dmLookupListBusinessObject.Timeout, boLookupList.TimeOut);
            Assert.AreEqual(dmLookupListBusinessObject.DMSortOrder.ToString(), boLookupList.SortString);
        }


        [Test]
        public void Test_RuleAndParameterConversion()
        {
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            DMPropRule propRuleInfo1 = new DMPropRule
                                           {
                                               Assembly = this.GetType().Assembly.GetName().Name,
                                               Class = typeof (PropRuleStub).FullName,
                                               PropRuleName = TestUtilsShared.GetRandomString(),
                                               Message = TestUtilsShared.GetRandomString()
                                           };
            DMPropRuleParameter param1 = AddPropRuleParameter(propRuleInfo1, "Param1", "ParamValue1");
            @property.DefaultPropRule = propRuleInfo1;
            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            IPropDef propDef = classDef.PropDefcol[@property.PropertyName];
            Assert.AreEqual(1, propDef.PropRules.Count);
            PropRuleBase propRule1 = (PropRuleBase) propDef.PropRules[0];
            Assert.IsInstanceOf(typeof (PropRuleStub), propRule1);
            Assert.AreEqual(propRuleInfo1.PropRuleName, propRule1.Name);
            Assert.AreEqual(propRuleInfo1.Message, propRule1.Message);
            Assert.AreEqual(1, propRule1.Parameters.Count);
            Assert.IsTrue(propRule1.Parameters.ContainsKey(param1.KeyName));
            Assert.AreEqual(param1.Value, propRule1.Parameters[param1.KeyName]);
        }

        //TODO: lookup lists

        [Test]
        public void Test_KeyConversion_ShouldCreateKeyDefWith2Props()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty property1 = dmClass.Properties[0];
            DMProperty property2 = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            DMUniqueConstraint uniqueConstraint = new DMUniqueConstraint
                                                      {
                                                          UniqueConstraintName = TestUtilsShared.GetRandomString(),
                                                          Message = TestUtilsShared.GetRandomString()
                                                      };
            DMUniqueConstraintProperty ucProp1 = new DMUniqueConstraintProperty {Property = property2};
            uniqueConstraint.UniqueConstraintProperties.Add(ucProp1);
            DMUniqueConstraintProperty ucProp2 = new DMUniqueConstraintProperty {Property = property1};
            uniqueConstraint.UniqueConstraintProperties.Add(ucProp2);
            uniqueConstraint.IgnoreIfNull = true;
            dmClass.UniqueConstraints.Add(uniqueConstraint);
            //---------------Assert Preconditions---------------
            Assert.AreEqual(2, uniqueConstraint.Properties.Count);
            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(1, classDef.KeysCol.Count);
            KeyDef keyDef = (KeyDef) classDef.KeysCol.ElementAt(0);
            Assert.IsNotNull(keyDef);
            Assert.AreEqual(2, keyDef.Count);
            StringAssert.Contains(uniqueConstraint.UniqueConstraintName, keyDef.KeyName);
            Assert.AreEqual(uniqueConstraint.Message, keyDef.Message);
            Assert.IsTrue(keyDef.IgnoreIfNull);
        }

        [Test]
        public void Test_PrimaryKeyConversion()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            dmClass.ObjectIdentity.Properties.Add(@property);
            dmClass.ObjectIdentity.IsObjectID = false;

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.PrimaryKeyDef.Count);
            Assert.IsNotNull(classDef.PrimaryKeyDef[@property.PropertyName]);
            Assert.IsNotNull(classDef.PrimaryKeyDef[dmClass.Properties[0].PropertyName]);
        }

        //TODO: Need to add reverseRelationShip to the Domain Model i.e DMRelationShip
        //TODO: Add owningBoHasForeignKey after SURI fixes the Model
        //TODO: Change the IModelRelationship and template to support multiple relatedProperties.

        [Test]
        public void Test_RelationshipConversion()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            DMClass relatedClass = TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);
            DMProperty relatedProperty = TestUtilsLogic.AddPropertyWithNoRules(relatedClass);
            DMRelationship dmRelationship = new DMRelationship();
            dmRelationship.RelatedClass = relatedClass;
            dmRelationship.RelationshipType = RelationshipType.Association;
            dmRelationship.Cardinality = Cardinality.Single;
            dmRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.ReverseRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            const DeleteParentAction deleteAction = DeleteParentAction.Prevent;
            dmRelationship.DeleteAction = deleteAction;
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty
                                                                {
                                                                    OwnerProperty = @property,
                                                                    RelatedProperty = relatedProperty
                                                                };
            dmRelationship.RelationshipProperties.Add(dmRelationshipProperty);
            dmClass.OwnerRelationships.Add(dmRelationship);

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];
            IRelationshipDef relationshipDef = classDef.RelationshipDefCol[dmRelationship.RelationshipName];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDefs.Count);
            Assert.IsNotNull(relationshipDef);
            Assert.AreEqual(dmRelationship.RelationshipName, relationshipDef.RelationshipName);
            Assert.AreEqual(deleteAction, relationshipDef.DeleteParentAction);
            Assert.AreEqual(relationshipDef.RelatedObjectClassName, relatedClass.ClassNameBO);
            Assert.AreEqual(RelationshipType.Association, relationshipDef.RelationshipType);
            Assert.IsInstanceOf(typeof (SingleRelationshipDef), relationshipDef);
        }

        [Test]
        public void Test_RelationshipConversion_MultipleRelationship()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            DMClass relatedClass = TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);
            DMProperty relatedProperty = TestUtilsLogic.AddPropertyWithNoRules(relatedClass);
            DMRelationship dmRelationship = new DMRelationship();
            dmRelationship.RelatedClass = relatedClass;
            dmRelationship.RelationshipType = RelationshipType.Composition;
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.ReverseRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            const DeleteParentAction deleteAction = DeleteParentAction.DoNothing;
            dmRelationship.DeleteAction = deleteAction;
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty
                                                                {
                                                                    OwnerProperty = @property,
                                                                    RelatedProperty = relatedProperty
                                                                };
            dmRelationship.RelationshipProperties.Add(dmRelationshipProperty);
            dmClass.OwnerRelationships.Add(dmRelationship);
            //dmRelationship.OrderBy = @property.PropertyName;
            DMSortOrder dmSortOrder = new DMSortOrder();
            DMSortProperty dmSortProperty = new DMSortProperty();
            dmSortProperty.PropertyName = @property.PropertyName;
            dmSortOrder.DMSortProperties.Add(dmSortProperty);
            dmRelationship.DMSortOrder = dmSortOrder;

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];
            IRelationshipDef relationshipDef = classDef.RelationshipDefCol[dmRelationship.RelationshipName];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDefs.Count);
            Assert.IsNotNull(relationshipDef);
            Assert.AreEqual(deleteAction, relationshipDef.DeleteParentAction);
            Assert.AreEqual(relationshipDef.RelatedObjectClassName, relatedClass.ClassNameBO);
            Assert.AreEqual(RelationshipType.Composition, relationshipDef.RelationshipType);
            Assert.IsInstanceOf(typeof (MultipleRelationshipDef), relationshipDef);
            Assert.AreEqual(@property.PropertyName + " ASC", relationshipDef.OrderCriteria.ToString());
        }

        [Test]
        public void Test_RelationshipConversion_MultipleRelationship_NoSortOrder()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            DMProperty @property = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            DMClass relatedClass = TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);
            DMProperty relatedProperty = TestUtilsLogic.AddPropertyWithNoRules(relatedClass);
            DMRelationship dmRelationship = new DMRelationship();
            dmRelationship.RelatedClass = relatedClass;
            dmRelationship.RelationshipType = RelationshipType.Composition;
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.ReverseRelationship.RelationshipName = TestUtilsShared.GetRandomString();
            const DeleteParentAction deleteAction = DeleteParentAction.DoNothing;
            dmRelationship.DeleteAction = deleteAction;
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty
            {
                OwnerProperty = @property,
                RelatedProperty = relatedProperty
            };
            dmRelationship.RelationshipProperties.Add(dmRelationshipProperty);
            dmClass.OwnerRelationships.Add(dmRelationship);

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];
            IRelationshipDef relationshipDef = classDef.RelationshipDefCol[dmRelationship.RelationshipName];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDefs.Count);
            Assert.IsNotNull(relationshipDef);
            Assert.AreEqual(deleteAction, relationshipDef.DeleteParentAction);
            Assert.AreEqual(relationshipDef.RelatedObjectClassName, relatedClass.ClassNameBO);
            Assert.AreEqual(RelationshipType.Composition, relationshipDef.RelationshipType);
            Assert.IsInstanceOf(typeof(MultipleRelationshipDef), relationshipDef);
            Assert.AreEqual("", relationshipDef.OrderCriteria.ToString());
        }


        [Test]
        public void Test_UIViewConversion()
        {
            //---------------Set up test pack-------------------  
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            UIView view = new UIView { ViewName = TestUtilsShared.GetRandomString() };
            dmClass.UIViews.Add(view);
            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(1, classDef.UIDefCol.Count);
            IUIDef uiDef = classDef.UIDefCol[view.ViewName];
            Assert.AreEqual(view.ViewName, uiDef.Name);
        }

        [Test]
        public void Test_TwoUIViewsConversion()
        {
            //---------------Set up test pack-------------------  
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            UIView view = new UIView { ViewName = TestUtilsShared.GetRandomString() };
            dmClass.UIViews.Add(view);
            UIView view2 = new UIView { ViewName = TestUtilsShared.GetRandomString() };
            dmClass.UIViews.Add(view2);

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDef.UIDefCol.Count);
            IUIDef uiDef1 = classDef.UIDefCol[view.ViewName];
            Assert.AreEqual(view.ViewName, uiDef1.Name);
            IUIDef uiDef2 = classDef.UIDefCol[view2.ViewName];
            Assert.AreEqual(view2.ViewName, uiDef2.Name);
        }





        [Test]
        public void Test_TwoClassesConversion()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            TestUtilsLogic.CreateClassWithOneGuidProperty(dmClass.Assembly);

            //---------------Execute Test ----------------------
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);

            //---------------Test Result -----------------------
            Assert.AreEqual(2, classDefs.Count);
        }

        //TODO Brett 18 Apr 2009: Peter the converter is doing some wild stuff with removing the 
        // Properties from the ClassDef.
        // Removing the origional Primary Key is going to cause major issues when you try
        // and import a DataSet its not gonna find a business object.
        //Also the PrimaryKeyDef is not being reset to an IsObjectId = true resulting in the 
        // EntityID not being reset.
        [Test]
        public void Test_Instantiate_Entity()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
            dmClass.ClassNameBO = typeof (Entity).Name;
            dmClass.Assembly.AssemblyName = typeof (Entity).Assembly.GetName().Name;
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.Name, dmClass.ClassNameBO];

            //---------------Execute Test ----------------------
            EntityStub entity = new EntityStub((ClassDef) classDef);

            //---------------Test Result -----------------------
            Assert.IsNotNull(entity);
            Assert.AreSame(classDef, entity.ClassDef);
            Assert.AreEqual(classDef.PropDefcol.Count, entity.Props.Count);
        }


        private static DMPropRuleParameter AddPropRuleParameter(DMPropRule propRule, string key, string value)
        {
            DMPropRuleParameter param = new DMPropRuleParameter {KeyName = key, Value = value};
            propRule.PropRuleParameters.Add(param);
            return param;
        }

       
    }

   

    
}